package com.example.himalaya.presenters;

import androidx.annotation.Nullable;

import com.example.himalaya.data.XimalayaApi;
import com.example.himalaya.interfaces.ISearchPresenter;
import com.example.himalaya.interfaces.ISearchViewCallback;
import com.example.himalaya.utils.Constants;
import com.example.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.SearchAlbumList;
import com.ximalaya.ting.android.opensdk.model.word.HotWord;
import com.ximalaya.ting.android.opensdk.model.word.HotWordList;
import com.ximalaya.ting.android.opensdk.model.word.QueryResult;
import com.ximalaya.ting.android.opensdk.model.word.SuggestWords;

import java.util.ArrayList;
import java.util.List;

public class SearchPresenter implements ISearchPresenter {
    private static final String TAG = "SearchPresenter";
    private static ISearchPresenter sSearchPresenter;
    private String mCurrentKeyword;
    private final XimalayaApi mXimalayaApi;
    private static final int DEFAULT_PAGE = 1;
    private int mCurrentPage = DEFAULT_PAGE;

    private List<Album> mAlbumList = new ArrayList<>();

    private boolean mIsLoadMore = false;

    private SearchPresenter() {
        mXimalayaApi = XimalayaApi.getmXimalayaApi();
    }

    public static ISearchPresenter getSearchPresenter() {
        if (sSearchPresenter ==null) {
            synchronized (ISearchPresenter.class) {
                if (sSearchPresenter ==null) {
                    sSearchPresenter = new SearchPresenter();
                }
            }
        }
        return sSearchPresenter;
    }

    private List<ISearchViewCallback> mSearchViewCallbacks = new ArrayList<>();

    @Override

    public void registerViewCallBack(ISearchViewCallback iSearchViewCallback) {
        if (!mSearchViewCallbacks.contains(iSearchViewCallback)) {
            mSearchViewCallbacks.add(iSearchViewCallback);
        }
    }

    @Override
    public void unRegisterViewCallBack(ISearchViewCallback iSearchViewCallback) {
        if (mSearchViewCallbacks != null) {
            mSearchViewCallbacks.remove(iSearchViewCallback);
        }
    }

    @Override
    public void doSearch(String keyword) {
        mCurrentPage = DEFAULT_PAGE;
        mIsLoadMore = false;
        mAlbumList.clear();
        updateLoading();
        this.mCurrentKeyword = keyword;
        search(keyword);
    }

    private void search(String keyword) {
        mXimalayaApi.searchByKeyword(keyword, mCurrentPage, new IDataCallBack<SearchAlbumList>() {
            @Override
            public void onSuccess(@Nullable SearchAlbumList searchAlbumList) {
                List<Album> albums = searchAlbumList.getAlbums();
                mAlbumList.addAll(albums);
                if (mIsLoadMore) {
                    if (mSearchViewCallbacks != null) {
                        for (ISearchViewCallback searchViewCallback : mSearchViewCallbacks) {
                            searchViewCallback.onLoadMoreResultLoad(mAlbumList, albums.size() != 0);
                        }
                    }
                    mIsLoadMore = false;
                }else {
                    if (mSearchViewCallbacks != null) {
                        for (ISearchViewCallback searchViewCallback : mSearchViewCallbacks) {
                            if (mIsLoadMore) {
                                searchViewCallback.onLoadMoreResultLoad(mAlbumList,false);
                                mIsLoadMore = false;
                            }
                            searchViewCallback.onSearchResultLoad(mAlbumList);
                        }
                    }
                }
            }

            @Override
            public void onError(int i, String s) {
                mCurrentPage--;
                LogUtil.d(TAG, "错误码是 -->" + i);
                LogUtil.d(TAG, "错误信息是 -->" + s);
                if (mSearchViewCallbacks != null) {
                    for (ISearchViewCallback searchViewCallback : mSearchViewCallbacks) {
                        searchViewCallback.onLoadNetworkError();
                    }
                }
            }
        });
    }

    private void updateLoading() {
        if (mSearchViewCallbacks != null) {
            for (ISearchViewCallback searchViewCallback : mSearchViewCallbacks) {
                searchViewCallback.onLoadLoading();
            }
        }
    }

    @Override
    public void reSearch() {
        mCurrentPage = DEFAULT_PAGE;
        mIsLoadMore = false;
        mAlbumList.clear();
        updateLoading();
        search(mCurrentKeyword);
    }

    @Override
    public void loadMore() {
        if (mAlbumList.size()< Constants.COUNT_ALBUM) {
            if (mSearchViewCallbacks != null) {
                for (ISearchViewCallback searchViewCallback : mSearchViewCallbacks) {
                    searchViewCallback.onLoadMoreResultLoad(mAlbumList,false);
                }
            }
        }else {
            mIsLoadMore = true;
            mCurrentPage++;
            search(mCurrentKeyword);
        }

    }

    @Override
    public void getHotWord() {
        //热词缓存

        mXimalayaApi.getHotWords(new IDataCallBack<HotWordList>() {
            @Override
            public void onSuccess(@Nullable HotWordList hotWordList) {
                List<HotWord> hotWords = hotWordList.getHotWordList();
                if (hotWords != null) {
                    if (mSearchViewCallbacks != null) {
                        for (ISearchViewCallback searchViewCallback : mSearchViewCallbacks) {
                            searchViewCallback.onHotWordLoad(hotWords);
                        }
                    }
                    LogUtil.d(TAG, "hotWords size -->" + hotWords.size());
                } else {
                    LogUtil.d(TAG, "hotWords is null" + hotWords.size());
                }
            }

            @Override
            public void onError(int i, String s) {
                LogUtil.d(TAG, "错误码是 -->" + i);
                LogUtil.d(TAG, "错误信息是 -->" + s);
            }
        });
    }

    @Override
    public void getSuggestWord(String keyword) {
        mXimalayaApi.getSuggestWord(keyword, new IDataCallBack<SuggestWords>() {
            @Override
            public void onSuccess(@Nullable SuggestWords suggestWords) {
                List<QueryResult> keyWordList = suggestWords.getKeyWordList();
                if (keyWordList != null) {
                    LogUtil.d(TAG, "keyWordList size -->" + keyWordList.size());
                    if (mSearchViewCallbacks != null) {
                        for (ISearchViewCallback searchViewCallback : mSearchViewCallbacks) {
                            searchViewCallback.onSuggestWordLoad(keyWordList);
                        }
                    }
                } else {
                    LogUtil.d(TAG, "keyWordList is null" + keyWordList.size());
                }
            }

            @Override
            public void onError(int i, String s) {
                LogUtil.d(TAG, "错误码是 -->" + i);
                LogUtil.d(TAG, "错误信息是 -->" + s);
            }
        });
    }
}
